package io;

import java.nio.IntBuffer;

public class Buffer_Test {
    public static void main(String[] args) {
        IntBuffer intBuffer = IntBuffer.allocate(10);
        sout(intBuffer);
        for (int i = 0; i < 6; i++) {
            intBuffer.put(i);
        }
        System.out.println("=====after put==========");
        sout(intBuffer);

        //flip buffer 进入读模式
        intBuffer.flip();
        System.out.println("=====after flip==========");
        sout(intBuffer);

        for (int i = 0; i < intBuffer.limit(); i++) {
            System.out.println(intBuffer.get());
            sout(intBuffer);
        }
        System.out.println("=====after read==========");
        sout(intBuffer);

        //倒带，读取之后重新读取
        intBuffer.rewind();
        //进入写模式
        intBuffer.clear();
        sout(intBuffer);
    }

    public static void sout(IntBuffer intBuffer) {
        System.out.println("--position---" + intBuffer.position());
        System.out.println("--capacity---" + intBuffer.capacity());
        System.out.println("--limit---" + intBuffer.limit());
        System.out.println("--mark---" + intBuffer.mark());
    }
}
